using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using QnA;
using System.Collections;
using System.Collections.Generic;

public partial class Chat : System.Web.UI.Page
{
    public static DateTime starttime;
    public static DateTime endtime;
    public static Boolean flag = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Session["user"] == null) || (Session["roomid"] == null))
            Response.Redirect("Default.aspx");

        if (Session["userID"].ToString().Equals(Session["roomid"].ToString()) || Session["roomid"].ToString().Equals("admin"))
        {
            btStartTime.Visible = false;
            btStop.Visible = false;
        }
        if (!IsPostBack)
        {

            ChatRoom room = ChatEngine.GetRoom(Session["roomid"].ToString());
            string prevMsgs = room.JoinRoom(Session["user"].ToString(), Session["user"].ToString());
            TextBox1.Text = prevMsgs;
            if (!Session["userID"].ToString().Equals(Session["roomid"].ToString()))
            {
                room.CustomerID = Int32.Parse(Session["userID"].ToString());
            }



        }
    }

    static public string SendMessage(string msg, string roomID)
    {
        try
        {
            ChatRoom room = ChatEngine.GetRoom(roomID);
            string res = "";
            if (room != null)
            {
                res = room.SendMessage(msg, HttpContext.Current.Session["user"].ToString());
            }
            return res;
        }
        catch (Exception ex)
        {

        }
        return "";
    }


    static public string UpdateUser(string roomID)
    {
        try
        {
            ChatRoom room = ChatEngine.GetRoom(roomID);
            if (room != null)
            {
                string res = "";
                if (room != null)
                {
                    res = room.UpdateUser(HttpContext.Current.Session["user"].ToString());
                    if (res != "") res += "\n";
                }
                return res;
            }
        }
        catch (Exception ex)
        {

        }
        return "";
    }


    /// <summary>
    /// This function is called from the client when the user is about to leave the room
    /// </summary>
    /// <param name="otherUser"></param>
    /// <returns></returns>

    static public string LeaveRoom(string roomID)
    {
        try
        {
            ChatRoom room = ChatEngine.GetRoom(roomID);
            if (room != null)
                room.LeaveRoom(HttpContext.Current.Session["user"].ToString());
        }
        catch (Exception ex)
        {

        }
        return "";
    }


    /// <summary>
    /// Returns a comma separated string containing the names of the users currently online
    /// </summary>
    /// <param name="roomID"></param>
    /// <returns></returns>





    protected void Button2_Click(object sender, EventArgs e)
    {
        String s = Chat.LeaveRoom(Session["roomid"].ToString());

        Response.Redirect("Default.aspx");
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        if (TextBox2.Text != "")
        {

            String s = SendMessage(TextBox2.Text, Session["roomid"].ToString());
            TextBox1.Text = s + "\n" + TextBox1.Text;
            TextBox2.Text = "";
            UpdatePanel3.Update();
        }

    }
    protected void Timer2_Tick(object sender, EventArgs e)
    {

        TextBox1.Text = UpdateUser(Session["roomid"].ToString()) + TextBox1.Text;
        UpdatePanel1.Update();



    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void btStartTime_Click(object sender, EventArgs e)
    {

        try
        {
            ChatRoom room = ChatEngine.GetRoom(Session["roomid"].ToString());
            if (room != null)
            {
                room.startTime = DateTime.Now;
                room.stone = true;
                btStartTime.Enabled = false;
                btStop.Enabled = true;
            }
        }
        catch (Exception starte)
        {

            throw starte;
        }

       
       

    }

protected void  Timer1_Tick(object sender, EventArgs e)
{
    ChatRoom room = ChatEngine.GetRoom(Session["roomid"].ToString());
    if (room != null)
    {
        if (room.stone)
        {
            TimeSpan t = DateTime.Now - room.startTime;
            String s = t.Hours + ":" + t.Minutes + ":" + t.Seconds;
            Label1.Text = s;
        }

        if (!room.stone)
        {
            
            String s = room.thoigianchat.Hours + ":" + room.thoigianchat.Minutes + ":" + room.thoigianchat.Seconds;
            Label2.Text = s;
            Label1.Text = "";
        }
    }
}

    protected void btStop_Click(object sender, EventArgs e)
    {

        ChatRoom room = ChatEngine.GetRoom(Session["roomid"].ToString());
        if (room != null)
        {
            room.stone = false;
            DateTime endtime = DateTime.Now;
            room.thoigianchat = endtime - room.startTime;
            String s = room.thoigianchat.Hours + ":" + room.thoigianchat.Minutes + ":" + room.thoigianchat.Seconds;
            Label2.Text = s;
            btStop.Enabled = false;
            Label1.Text = "";
        }

    }

    protected void btExit_Click(object sender, EventArgs e)
    {
       /*code to den cho form ket thuc*/
 
        LeaveRoom(Session["roomid"].ToString());
        Session["roomid"] = null;
        Response.Redirect("Default.aspx");
        
    }
}
